package com.tanhua.server.controller;

import com.tanhua.model.db.LikeCounts;
import com.tanhua.model.vo.MainCountVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.service.UserInfoService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * 用户信息管理 控制层
 */
@RestController
@RequestMapping("/users")
public class UserInfoController {


    @Autowired
    private UserInfoService userInfoService;

    /**
     * 用户信息管理-查询用户资料
     */
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity getUserInfo(Long userID){
        UserInfoVo userInfoVo = userInfoService.getUserInfo(userID);
        return ResponseEntity.ok(userInfoVo);
    }


    /**
     * 用户信息管理-更新用户资料
     */
    @RequestMapping(method = RequestMethod.PUT)
    public ResponseEntity updateUserInfo(@RequestBody UserInfoVo userInfoVo){
        userInfoService.updateUserInfo(userInfoVo);
        return ResponseEntity.ok(null);
    }

    /**
     * 用户信息管理-更新用户头像
     */
    @RequestMapping(value = "/header", method = RequestMethod.POST)
    public ResponseEntity updateHeader(MultipartFile headPhoto) {

        //根据userInfo 信息更新用户资料信息
        userInfoService.updateHeader(headPhoto);
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号-1.发送短信验证
     */
    @RequestMapping(value = "/phone/sendVerificationCode",method = RequestMethod.POST)
    public ResponseEntity sendVerificationCode(){
        //调用服务获取验证码
        userInfoService.sendVerificationCode();

        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号 - 2 校验验证码
     */
    @RequestMapping(value = "/phone/checkVerificationCode",method = RequestMethod.POST)
    public ResponseEntity checkVerificationCode(@RequestBody Map<String,String> param){
        //调用服务获取验证码
        Map<String,Boolean> map = new HashedMap();
        Boolean verification = userInfoService.checkVerificationCode(param);
        map.put("verification",verification);
        return ResponseEntity.ok(map);
    }

    /**
     * 修改手机号 - 3 保存
     */
    @RequestMapping(value = "/phone",method = RequestMethod.POST)
    public ResponseEntity changePhone(@RequestBody Map<String,String> param){
        //调用服务获取验证码
        userInfoService.changePhone(param);

        return ResponseEntity.ok(null);
    }


    /**
     * 互相喜欢，喜欢，粉丝 - 统计
     * @return
     */
    @RequestMapping(value = "/counts",method = RequestMethod.GET)
    public ResponseEntity counts(){
        LikeCounts likeCounts  = userInfoService.counts();
        return ResponseEntity.ok(likeCounts);
    }


    /**
     * 互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     */
    @RequestMapping(value = "/friends/{type}",method = RequestMethod.GET)
    public ResponseEntity mainCount(@PathVariable("type") Integer type,@RequestParam(value = "page", defaultValue = "1") Long page,
                                    @RequestParam(value = "pageSize", defaultValue = "10")Long pageSize,
                                    String nickname){

    PageResult<MainCountVo> pageResult= userInfoService.mainCount(type,page,pageSize,nickname);

    return ResponseEntity.ok(pageResult);
    }
}
